﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;

namespace MVCApplication.Helpers
{
    public class GeneralFunctions
    {
        public static string GetThemePath(System.Web.UI.Page page)
        {
            return GetThemePath(page, true);
        }

        public static string[] GetSplittedText(string text)
        {
            if (!string.IsNullOrEmpty(text))
            {
                List<string> returnValuesAux = Split(text, "-,-");
                if (returnValuesAux.Count > 0)
                {
                    string[] returnValues = new string[returnValuesAux.Count];
                    int index = 0;
                    foreach (string value in returnValuesAux)
                    {
                        returnValues[index++] = value.Replace(",,", ",");
                    }
                    return returnValues;
                }
                else
                {
                    return null;
                }
            }
            else
            {
                return null;
            }
        }

        public static List<string> Split(string text, string separator)
        {
            List<string> returnValues = new List<string>();
            int startIndex = 0;
            while (startIndex < text.Length)
            {
                int separatorIndex = text.IndexOf(separator, startIndex);
                if (separatorIndex != -1)
                {
                    string word = text.Substring(startIndex, separatorIndex - startIndex);
                    if (word.Length > 0)
                    {
                        returnValues.Add(word);
                    }
                    startIndex = separatorIndex + separator.Length;
                }
                else
                {
                    string word = text.Substring(startIndex);
                    returnValues.Add(word);
                    startIndex = text.Length;
                }
            }
            return returnValues;
        }
        
        public static string GetThemePath(System.Web.UI.Page page, bool fromSession)
        {
			if(fromSession)
			{
				return (string)page.Session["ThemePath"];
			}
			else
			{
				string result = "~"; //ApplicationPath;
				if (page.Theme != null)
				{
					result += "/App_Themes/" + page.Theme + "/";
				}
				else if (page.StyleSheetTheme != null)
				{
					result += "/App_Themes/" + page.StyleSheetTheme + "/";
				}
				return page.ResolveUrl(result);
			}
        }
        
		 /// <summary>
		/// Changes the .DefaultValue property to an 'Empty' value for all the fields in a DataColumn that
		/// have a System.Convert.DBNull value.
		/// </summary>
		/// <param name="table"></param>
		public static void ChangeTablesDefaults(System.Data.DataTable table)
		{
			foreach (DataColumn dataColumn in table.Columns)
			{							
				if (dataColumn.DefaultValue.Equals(System.Convert.DBNull) && (!dataColumn.AutoIncrement) && ( (!dataColumn.AllowDBNull) || (dataColumn.DataType.Equals( typeof(System.Boolean))) || dataColumn.DataType.Equals(typeof(System.Byte[])) ) && !(dataColumn.DataType == typeof(System.Guid) && dataColumn.ExtendedProperties["AutoGenerated"]!=null && ((string)dataColumn.ExtendedProperties["AutoGenerated"]).ToLower() == "true") ) 
				{
					dataColumn.DefaultValue = EmptyValue(dataColumn.DataType);
				}
			}
		}
		
		/// <summary>
		/// Returns an 'Empty' value for a given System.Type
		/// </summary>
		/// <param name="dataType"></param>
		private static object EmptyValue(System.Type dataType)
		{
			if	(dataType.Equals(typeof(System.Int16)) || 
				dataType.Equals(typeof(System.Int32)) || 
				dataType.Equals(typeof(System.Int64)) ||
				dataType.Equals(typeof(System.Double)) ||
				dataType.Equals(typeof(System.Decimal)) ||
				dataType.Equals(typeof(System.Single))
				)
			{
				return 0;
			}
			else if (dataType.Equals(typeof(System.String)))
			{
				return "";
			}
			else if (dataType.Equals(typeof(System.DateTime)))
			{
				return System.DateTime.Today;
			}
			else if (dataType.Equals(typeof(System.Boolean)))
			{
				return false;
			}
			else if (dataType.Equals(typeof(System.Byte[])))
			{
				return new System.Byte[0];
			}
			else if (dataType.Equals(typeof(System.Guid )))
			{
				return System.Guid.Empty ;
			}
			
			throw new System.Exception(String.Format(Deklarit.Resources.Resources.notSupportedType , dataType));
		}

        /// <summary>
        /// Returns all generated objects
        /// </summary>
        /// <param name="objects"></param>
        /// <param name="objectDescriptions"></param>
        public static void GetAllObjects(out string[] objects, out string[] objectDescriptions)
        {
            objects = new string[] { "Ciudad", "Cliente", "Invoice", "Pais", "Product" };
            objectDescriptions = new string[] { "Ciudad", "Cliente", "Invoice", "Pais", "Product" };
        }
    }
}
